package com.dingding.service.user.core.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.base.dto.BaseDTO;
import com.dingding.common.core.beans.SyncUserData;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.service.user.core.dao.ISyncUserDataDao;
import com.dingding.service.user.core.sql.SyncUserDataSQL;

/**
 * 
 * @ClassName: ISyncUserDataDao 
 * @Description: 第三方平台用户数据表操作DAO
 * @author CHEHAUNBO 
 * @date 2016年4月22日 下午3:50:01
 * 
 */
@Component("syncUserDataDao")
public class SyncUserDataDaoImpl extends BaseDaoImpl<SyncUserData> implements ISyncUserDataDao {

	public static final Logger log = Logger.getLogger(SyncUserDataDaoImpl.class);

	/**
	 * 
	 * 同步第三方平台用户数据
	 * 
	 * 当前方法只同步手机号
	 * 
	 * @Title: addSyncUserData 
	 * @param syncUserDatas 待同步的用户数据集合
	 * @return boolean true 成功 false 失败
	 * @author CHEHAUNBO
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean addSyncUserData(List<String> syncUserDatas) {

		try {
//			System.out.println("调用一次");
			// 添加数据
			for (String userData : syncUserDatas) {
				SyncUserData syncUserData = new SyncUserData();
				syncUserData.setSyncDataId(ToolUtil.uuid());
				syncUserData.setTelPhone(userData);
				super.save(syncUserData);
			}
			return true;
		} catch (Exception e) {
			log.error("同步第三方平台用户数据到叮叮数据库中出现异常", e);
			return false;
		}

	}

	/**
	 * 
	 * 查询所有已经同步成功的用户数据
	 * 
	 * @Title: queryAllSyncUserData 
	 * @return  List<SyncUserData> 同步成功的第三方平台的用户数据
	 * @author CHEHAUNBO
	 * 
	 */
	@Override
	@Transactional(readOnly = true)
	public BaseDTO<List<SyncUserData>> queryAllSyncUserData(BaseDTO<List<SyncUserData>> baseDTOParm) {

		int currentPage = baseDTOParm.getCurrentPage(); // 当前页
		int pageSize = baseDTOParm.getPageSize();// 每页显示记录数

		BaseDTO<List<SyncUserData>> baseDTO = new BaseDTO<List<SyncUserData>>();

		try {
			List<SyncUserData> syncUserDatas = super.listHqlByPage(SyncUserDataSQL.QUERY_SYNCUSERDATA_BY_PAGE,
					currentPage, pageSize);
			baseDTO.setObj(syncUserDatas);
			return baseDTO;
		} catch (Exception e) {
			log.error("查询所有已经同步成功的用户数据出现异常！", e);
		}
		return null;
	}

}
